package com.fx.ruankao.controller;

import com.fx.ruankao.entity.Role;
import com.fx.ruankao.service.RoleService;
import com.fx.ruankao.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 角色控制器 - 管理端
 */
@RestController
@RequestMapping("/api/admin/roles")
public class RoleController {
    
    @Autowired
    private RoleService roleService;
    
    /**
     * 新增角色
     */
    @PostMapping
    public Result insert(@RequestBody Role role) {
        try {
            int result = roleService.insert(role);
            if (result > 0) {
                return Result.success("新增角色成功");
            } else {
                return Result.error("新增角色失败");
            }
        } catch (Exception e) {
            return Result.error("系统异常：" + e.getMessage());
        }
    }
    
    /**
     * 根据ID删除角色
     */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        try {
            int result = roleService.deleteById(id);
            if (result > 0) {
                return Result.success("删除角色成功");
            } else {
                return Result.error("删除角色失败");
            }
        } catch (Exception e) {
            return Result.error("系统异常：" + e.getMessage());
        }
    }
    
    /**
     * 更新角色
     */
    @PutMapping
    public Result update(@RequestBody Role role) {
        try {
            int result = roleService.update(role);
            if (result > 0) {
                return Result.success("更新角色成功");
            } else {
                return Result.error("更新角色失败");
            }
        } catch (Exception e) {
            return Result.error("系统异常：" + e.getMessage());
        }
    }
    
    /**
     * 根据ID查询角色
     */
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id) {
        try {
            Role role = roleService.selectById(id);
            if (role != null) {
                return Result.success(role);
            } else {
                return Result.error("未找到角色");
            }
        } catch (Exception e) {
            return Result.error("系统异常：" + e.getMessage());
        }
    }
    
    /**
     * 查询所有角色
     */
    @GetMapping
    public Result selectAll() {
        try {
            List<Role> roles = roleService.selectAll();
            return Result.success(roles);
        } catch (Exception e) {
            return Result.error("系统异常：" + e.getMessage());
        }
    }
    
    /**
     * 批量删除角色
     */
    @DeleteMapping("/batch")
    public Result batchDelete(@RequestBody Integer[] ids) {
        try {
            int result = roleService.batchDelete(ids);
            if (result > 0) {
                return Result.success("批量删除角色成功");
            } else {
                return Result.error("批量删除角色失败");
            }
        } catch (Exception e) {
            return Result.error("系统异常：" + e.getMessage());
        }
    }
    
    /**
     * 为角色分配权限
     */
    @PostMapping("/{roleId}/permissions")
    public Result assignPermissions(@PathVariable Integer roleId, @RequestBody Integer[] permissionIds) {
        try {
            boolean result = roleService.assignPermissions(roleId, permissionIds);
            if (result) {
                return Result.success("分配权限成功");
            } else {
                return Result.error("分配权限失败");
            }
        } catch (Exception e) {
            return Result.error("系统异常：" + e.getMessage());
        }
    }
    
    /**
     * 根据用户ID查询角色列表
     */
    @GetMapping("/user/{userId}")
    public Result selectByUserId(@PathVariable Integer userId) {
        try {
            List<Role> roles = roleService.selectByUserId(userId);
            return Result.success(roles);
        } catch (Exception e) {
            return Result.error("系统异常：" + e.getMessage());
        }
    }
    

}